package source
{
	import com.justinbuser.net.DynamicURLLoader;
	import com.justinbuser.net.PollingClient;
	import com.justinbuser.net.PollingConnection;
	import com.justinbuser.singleton.Core;
	
	import flash.events.Event;
	import flash.events.TouchEvent;
	import flash.geom.Rectangle;
	import flash.media.StageWebView;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.net.sendToURL;
	
	import mx.collections.ArrayList;
	import mx.core.FlexGlobals;
	
	import source.MainApplication;
	
	import spark.components.List;
	import spark.components.View;
	import spark.components.ViewMenuItem;
	
	import views.Login;
	import views.Reports;
	
	[Event(name="loginRequired", type="flash.events.Event")]
	
	[Bindable] public class ReportsView extends View implements PollingClient
	{
		public var poller:PollingConnection;
		protected var core:Core = Core.core;
		public var reports:List;
		private var lastReport:String;
		private var lastY:Number;
		public var loadSavedReportsMenuItem:ViewMenuItem;
		
		public function ReportsView()
		{
			super();
			trace('reports view instantiated');
			poller = new PollingConnection("http://battlelog.battlefield.com/bf3/battlereport/loadLatestPlayerPreviews/",this,core.autoUpdate,core.pollingInterval);
			viewMenuItems = MainApplication.mainViewMenuItems;
			//http://battlelog.battlefield.com/bf3/gate/login/?redirect=|bf3|battlereport|loadLatestPlayerPreviews|10|&email=backdoorcharley@boogiemanclan.com&password=bertani
		}
		
		public function init():void{
			if(data && data is ArrayList)
			{
				reports.dataProvider = ArrayList(data);
				return;
			}
			viewMenuItems = MainApplication.mainViewMenuItems.concat();
			viewMenuItems.unshift(loadSavedReportsMenuItem);
			/*poller.autoStart = core.autoUpdate;
			poller.interval = core.pollingInterval;*/
				if(core.autoUpdate){
					trace('polling start');
					poller.start();
				}
				else{ 
					trace('poller load');
					poller.load();
				}
		}
		
		public function stop():void{
			poller.stop();
		}
		
		public function loadSavedReports():void{
			navigator.pushView(views.SavedReports,new ArrayList(core.savedReports));
		}
		
		public function showReport():void{
			navigator.pushView(views.FullReport,reports.selectedItem);
		}
		
		
		public function logout():void{
			trace('logging out');
			poller.stop();
			var loader:DynamicURLLoader = new DynamicURLLoader(showLoginView);
			loader.open('http://battlelog.battlefield.com/bf3/session/logout/',null,2,"GET");
			core.authenticated = false;
			
			function showLoginView(e:Event):void{
				core.authenticated = false;
				navigator.popAll(null);
				navigator.pushView(views.Login);
			}
			
		}
		
		private function populateList(data:Object):void{
			if(core.translations)
			{
				var previews:Array = data.data.previews;
				var dp:ArrayList = new ArrayList();
				for(var i:int = 0; i<previews.length; i++)
				{
					dp.addItem(new BattleReport(previews[i]));
				}
				reports.dataProvider = dp;
				reports.visible = true;
			}
			else
			{
				callLater(populateList,[data]);
			}
		}
		
		public function refresh():void{
			poller.load(null);
		}
		
		public function dataReceived(data:Object):void{
			trace('data received');
			var response:Object;
			var currentReport:String = String(data);
			try{
				response = JSON.parse(String(data));
				core.authenticated=true;
				if(lastReport != currentReport)
				{
					trace('new report');
					populateList(response);
					lastReport = currentReport;	
				}
			}
			catch(error:SyntaxError){
				poller.stop();
				core.authenticated=false;
				dispatchEvent(new Event('loginRequired'));
				if(navigator.length>1)
					navigator.popView();
				else
					navigator.pushView(views.Login);
			}
		}
		
		public function connectionError(e:Error):void{
			trace('connectionError:',e);
		}
	}
}